/**
 * Shopro-request
 * @description api模块管理，loading配置，请求拦截，错误处理
 */

// import Request from "luch-request";
import axios from "axios";


/**
 * @description 请求拦截器
 */

axios.interceptors.request.use(
  
  (config) => {
    console.log('config',config)
    const token = 'uni.getStorageSync("token")';
    if (token) {
      config.headers["token"] = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * @description 响应拦截器
 */
axios.interceptors.response.use(
  (response) => {
    if (response.data.code !== 1) {
      if (response.config.custom.showError)
        console.log(response.data.msg || "服务器开小差啦,请稍后再试~");
      return Promise.resolve(response.data);
    }
    if (
      response.data.code === 1 &&
      response.data.msg !== "" &&
      response.config.custom.showSuccess
    ) {
      console.log(response.config.custom.successMsg || response.data.msg);
    }
    return Promise.resolve(response.data);
  },
  (error) => {
    console.log('~~~~~error',error);
    const isLogin = true;
    let errorMessage = "网络请求出错";
    if (error !== undefined) {
      switch (error.statusCode) {
        case 400:
          errorMessage = "请求错误";
          break;
        case 401:
          if (isLogin) {
            errorMessage = "您的登陆已过期";
          } else {
            errorMessage = "请先登录";
          }
          break;
        case 403:
          errorMessage = "拒绝访问";
          break;
        case 404:
          errorMessage = "请求出错";
          break;
        case 408:
          errorMessage = "请求超时";
          break;
        case 429:
          errorMessage = "请求频繁, 请稍后再访问";
          break;
        case 500:
          errorMessage = "服务器开小差啦,请稍后再试~";
          break;
        case 501:
          errorMessage = "服务未实现";
          break;
        case 502:
          errorMessage = "网络错误";
          break;
        case 503:
          errorMessage = "服务不可用";
          break;
        case 504:
          errorMessage = "网络超时";
          break;
        case 505:
          errorMessage = "HTTP版本不受支持";
          break;
      }
      console.log(errorMessage);
    }

    return false;
  }
);
export default axios

